package com.vita.bduck;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import com.vita.bduck.annotation.view.EventListener;
import com.vita.bduck.annotation.view.Select;
import com.vita.bduck.annotation.view.ViewInject;

import java.lang.reflect.Field;

/**
 * 操作基本Activity界面主类
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:32
 * To change this template use File | Settings | File Templates.
 */
public class BDuckActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);    //To change body of overridden methods use File | Settings | File Templates.
        initView();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);    //To change body of overridden methods use File | Settings | File Templates.
        initView();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);    //To change body of overridden methods use File | Settings | File Templates.
        initView();
    }

    private void initView() {
        Field[] fields = getClass().getDeclaredFields();
        if(fields != null && fields.length > 0){
            for (Field field : fields) {
                ViewInject viewInject = field.getAnnotation(ViewInject.class);
                if (viewInject != null){
                    int viewId = viewInject.id();
                    try {
                        field.setAccessible(true);
                        field.set(this,findViewById(viewId));
                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    String clickMethod = viewInject.click();
                    if(!TextUtils.isEmpty(clickMethod))
                        setViewClickListener(field,clickMethod);

                    String longClickMethod = viewInject.longClick();
                    if(!TextUtils.isEmpty(longClickMethod))
                        setViewLongClickListener(field,longClickMethod);

                    String itemClickMethod = viewInject.itemClick();
                    if(!TextUtils.isEmpty(itemClickMethod))
                        setItemClickListener(field,itemClickMethod);

                    String itemLongClickMethod = viewInject.itemLongClick();
                    if(!TextUtils.isEmpty(itemLongClickMethod))
                        setItemLongClickListener(field,itemLongClickMethod);

                    Select select = viewInject.select();
                    if(!TextUtils.isEmpty(select.selected()))
                        setViewSelectListener(field,select.selected(),select.noSelected());
                }
            }
        }
    }

    private void setViewClickListener(Field field,String clickMethod){
        try{
            Object obj = field.get(this);
            if(obj instanceof View){
                ((View)obj).setOnClickListener(new EventListener(this).click(clickMethod));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void setViewLongClickListener(Field field,String clickMethod){
        try {
            Object obj = field.get(this);
            if(obj instanceof View){
                ((View)obj).setOnLongClickListener(new EventListener(this).longClick(clickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setItemClickListener(Field field,String itemClickMethod){
        try {
            Object obj = field.get(this);
            if(obj instanceof AbsListView){
                ((AbsListView)obj).setOnItemClickListener(new EventListener(this).itemClick(itemClickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setItemLongClickListener(Field field,String itemClickMethod){
        try {
            Object obj = field.get(this);
            if(obj instanceof AbsListView){
                ((AbsListView)obj).setOnItemLongClickListener(new EventListener(this).itemLongClick(itemClickMethod));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setViewSelectListener(Field field,String select,String noSelect){
        try {
            Object obj = field.get(this);
            if(obj instanceof View){
                ((AbsListView)obj).setOnItemSelectedListener(new EventListener(this).select(select).noSelect(noSelect));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}